package ccc.virus.login.controller;

import ccc.virus.activemq.QueueSender;
import ccc.virus.activemq.TopicSender;
import ccc.virus.common.utils.SecurityUtils;
import ccc.virus.user.entity.User;
import ccc.virus.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private QueueSender queueSender;
    @Autowired
    private TopicSender topicSender;

    /**
    * @Author:CCC
    * @Description:登陆页面
    * @Date:2018/8/3
    */
    @RequestMapping
    public String login(){


        return "login";
    }
    /**
    * @Author:CCC
    * @Description:登陆校验
    * @Date:2018/8/3
    */
    @RequestMapping("check")
    @ResponseBody
    public String checkLogin(HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = userService.selectUserByUsername(username);
        String data = "";
        if(user!=null){
            data = SecurityUtils.checkPassword(password,user.getPassword())?"login_succ":"login_fail";
            if(data.equals("login_succ")){
                request.getSession().setAttribute("userinfo",user);
            }
        }else{
            data = "login_fail";
        }

        return data;
    }


}
